/*******************************************
 * Copyright 2007 Aphrodite Framework for javaME .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * Email : owner@aphroditeme.dev.java.net
 * Contributors:
 *     Kent Elton - initial API and implementation
 *******************************************/

package com.aphrodite.ui.widgets;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

import com.aphrodite.util.BitmapFont;
import com.aphrodite.util.geom.GeomUtil;

/**
 * Label wigdet.
 * 
 * @author kentelt
 * 
 */
public class Label extends TraversableWidget {

	/**
	 * HyperLink type look.
	 * <p>
	 * <img src="../../../resources/label_link.png">
	 * </p>
	 * 
	 * @see Label#setType(byte)
	 */
	public static final byte TYPE_LINK = 10;

	/**
	 * Plain look.
	 * <p>
	 * <img src="../../../resources/label_plain.png">
	 * </p>
	 * 
	 * @see Label#setType(byte)
	 */
	public static final byte TYPE_PLAIN = 20;
	private String label;
	private int linkColor = 0x0000FF;

	private byte type = TYPE_PLAIN;

	public Label(Theme theme, String label) {
		super(theme, 0, 0);
		this.label = label;
		updateModel();
	}

	public void cleanUp() {
		label = null;
	}

	public final boolean keyPressed(int gameActionCode, int key) {
		switch (gameActionCode) {
		case Canvas.FIRE:
			super.fire();
			break;
		case Canvas.LEFT:
			isInFocus = false;
			break;
		case Canvas.RIGHT:
			isInFocus = false;
			break;
		case Canvas.DOWN:
			isInFocus = false;
			break;
		case Canvas.UP:
			isInFocus = false;
			break;
		}
		return false;
	}

	public final boolean pointerRelease(int px, int py) {
		if (isVisible && GeomUtil.withinBounds(px, py, x, y, width, height)) {
			super.fire();
			return true;
		}
		return false;
	}

	public void render(Graphics g) {
		if (!isVisible)
			return;
		capture(g, width, height, true);
		g.setColor(theme.fgColor);
		if (theme.bmFont != null) {
			if (type == TYPE_PLAIN)
				theme.bmFont.drawString(g, label, 2, 2, 0);
			else {
				g.setColor(linkColor);
				theme.bmFont.getFont(BitmapFont.STYLE_UNDERLINED).drawString(g, label, 2, 2, 0);
			}
		} else {
			if (type == TYPE_PLAIN) {
				g.setFont(theme.widgetFont);
				g.drawString(label, 2, 2, 0);
			} else {
				g.setColor(linkColor);
				g.setFont(Font.getFont(theme.widgetFont.getFace(), Font.STYLE_UNDERLINED, theme.widgetFont.getSize()));
				g.drawString(label, 2, 2, 0);
			}
		}
		release(g, width, height, true);

	}

	// public void setFont(javax.microedition.lcdui.Font font) {
	// super.setFont(font);
	// updateModel();
	// reDrawWidget();
	// stateChanged(this);
	// }

	public void setText(String text) {
		this.label = text;
		updateModel();
		reDrawWidget();
		stateChanged(this);
	}

	public void setType(byte type) {
		this.type = type;
		updateModel();
		reDrawWidget();
		stateChanged(this);
	}

	protected boolean traverse(int dir, int viewportWidth, int viewportHeight, int[] visRect_inout) {
		if (type == TYPE_LINK) {
			return true;
		} else
			return false;
	}

	protected void updateModel() {
		if (theme.bmFont != null) {
			this.width = theme.bmFont.stringWidth(label) + 4;
			this.height = theme.bmFont.getHeight() + 4;
		} else {
			this.width = theme.widgetFont.stringWidth(label) + 4;
			this.height = theme.widgetFont.getHeight() + 4;
		}

	}
}
